// packageC/pages/ccc/ccc.js
const common = require('../../../../public')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showCalendar: false,
    formatter: '',
    minDate: '',
    maxDate: '',
    appointmentDate: '',
    venueFieldId: '',
    currentField: '',
    money: 0,
    todayNum: -1,
    resultsArray: [],
    activeIndex: -1,//日期初始值
    fieldIndex: -1,//领域初始值
    timeIndex: -1,//时间初始值
    dateInfo: [],//日期列表
    fieldInfo: [],//场地列表
    IndexTime: ''
  },
  // 点击跳转到确定订单页面
  toConfrim() {
    let { activeNumber, money } = this.data
    if (this.data.activeIndex == -1 || this.data.fieldIndex == -1 || this.data.timeIndex == -1) {
      wx.showToast({
        title: '请选择详细的信息',
        icon: "none"
      })
      return
    } else {
      wx.navigateTo({
        url: '/packageA/pages/Goods/confirm/confirm?storeName=' + this.storeName + "&cityName=" + this.cityName + "&storeId=" + this.storeId + "&venueId=" + this.venueId + "&timePrice=" + this.timePrice + "&money=" + money + "&appointmentDate=" + this.data.appointmentDate + "&venueName=" + this.data.fieldInfo[this.data.fieldIndex].fieldName + "&venueTimeId=" + this.timeId + "&startTime=" + this.data.timeInfo[this.data.IndexTime].startTime + "&endTime=" + this.data.timeInfo[this.data.IndexTime].endTime + "&timePrice=" + this.data.timeInfo[this.data.IndexTime].price + "&venueFieldId=" + this.data.fieldInfo[this.data.fieldIndex].id + '&venuName=' + this.venuName,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    console.log(option)
    const that = this
    this.cityName = option.cityName
    this.venueId = option.venueId ? option.venueId : 24
    this.storeId = option.storeId
    this.storeName = option.storeName
    this.venuName = option.venuName
    this.geCoupList()
    this.getField()
    this.getTime()

  },

  // 券确定
  ComPany_submit() {
    this.setData({
      showCouponsPanel: false,
    })

  },

  selectDate(e) {
    const that = this
    let currentObj = e.currentTarget.dataset
    let todayNum = e.currentTarget.dataset.index
    this.dateIndex = this.data.dateInfo[e.currentTarget.dataset.index].Java
    // console.log(,"currentObjcurrentObjcurrentObj")

    this.setData({
      activeIndex: e.currentTarget.dataset.index,
      currenTicketDate: currentObj,
      appointmentDate: currentObj.item.Java,
      todayNum,

    }, () => {
      let ii = that.data.currenTicketDate ? that.data.currenTicketDate.item.Java : ''

      this.getTime(ii)
    })
  },
  // 获取日期
  geCoupList() {
    const tempJson = {
      venueId: this.venueId
    }

    const that = this
    common.post('/venue/getStoreVenueDates ', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          dateDisplayList: res.data,
          resultsArray: res.data,

        }, () => {

          for (let i = 0; i < that.data.dateDisplayList.length; i++) {

            var DateStr = that.data.dateDisplayList[i].split(' ')

            var datas = new Date(DateStr[0])
            var JsDate = datas.getTime()
            that.data.dateDisplayList[i] = {
              Java: that.data.dateDisplayList[i],
              Js: JsDate
            }
          }
          let length = that.data.dateDisplayList.length
          that.setData({
            minDate: that.data.dateDisplayList[0].Js,
            maxDate: that.data.dateDisplayList[length - 1].Js,
            dateDisplayList: that.data.dateDisplayList,
            resultsArray: that.data.dateDisplayList,
            dateInfo: that.data.dateDisplayList
            // dateInfo:that.data.dateDisplayList
          }, () => {

          })
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })


  },
  // 获取场地
  getField() {
    const that = this
    const tempJson = {
      venueId: this.venueId
    }
    common.post('/venue/getStoreVenueField ', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          fieldInfo: res.data
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  selectField(e) { //点击选择入园时间
    let that = this
    let currentObj = e.currentTarget.dataset
    console.log(e)

    this.setData({
      fieldIndex: e.currentTarget.dataset.index,
      currentField: currentObj,
      venueFieldId: e.currentTarget.dataset.id
    }, () => {
      console.log(that.data.currenTicketDate, "444444")
      let ii = that.data.currenTicketDate ? that.data.currenTicketDate.item.Java : ''
      this.getTime(ii)//获取可选时间
    })
  },
  selectTime(e) {
    let currentObj = e.currentTarget.dataset
    if (currentObj.item.stock == 0) {
      common.showToast('此场馆已被预订')
      return
    }
    if (currentObj.item.timeFlag == false) {
      common.showToast('当前时间不可选择')
      return
    }
    this.setData({
      money: this.timePrice,
      currentTime: currentObj,
      timeIndex: e.currentTarget.dataset.index,
      IndexTime: e.currentTarget.dataset.index
    }, () => {
      this.timeId = this.data.timeInfo[this.data.IndexTime].id
      this.timePrice = this.data.timeInfo[this.data.IndexTime].price
      this.setData({
        money: this.timePrice,
      })

    })
  },
  //可预约时间段
  getTime(todayNums = null) {
    const that = this
    const tempJson = {
      appointmentDate: this.data.appointmentDate,
      venueFieldId: this.data.venueFieldId,
      venueId: this.venueId
    }
    common.post('/venue/getStoreVenueTime ', tempJson, function (res) {
      if (res.status === 200) {
        var n = new Date().getTime()
        let today = common.dateFormat('yyyy-MM-dd', n,)

        let todays = today + ' '
        let ToDAY = common.parseTime(todays, '{y}-{m}-{d}  星期{a}',)

        console.log(ToDAY, "ToDAYToDAYToDAYToDAY")
        console.log(todayNums, "chuanlaijjjjj")

        var timeHour = new Date().getHours();
        var Minutes = new Date().getMinutes();
        var timeNow = timeHour.toString() + ':' + Minutes.toString()
        var Nowtime = that.time_to_sec(timeNow)
        var timeJava;
        for (let i = 0; i < res.data.length; i++) {

          res.data[i].timeFlag = true
          console.log(that.data.todayNum, "0000000000000000")
          console.log(ToDAY === todayNums, "0000000000000000")
          if (that.data.todayNum == 0 && ToDAY === todayNums) { // 判断是不是今天
            console.log("今天")

            var javaTime = res.data[i].endTime
            timeJava = that.time_to_sec(javaTime)
            var resultTime = that.compareTime(timeJava, Nowtime)
            if (!resultTime || res.data[i].stock == 0) {
              res.data[i].timeFlag = false
            }
          } else {
            console.log("不是今天")
            res.data[i].timeFlag = true
            if (res.data[i].stock == 0) {
              res.data[i].timeFlag = false
            }
          }
        }
        var obj = {
          index: -1,

        }

        that.setData({
          timeInfo: res.data,
          timeIndex: -1,
          currentTime: obj
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  ZeroFn(num) {
    if (num < 9) {
      return '0' + num
    }
  },
  // 日历确定
  onConfirm(e) {
    const that = this

    var n = new Date().getTime()
    let today = common.dateFormat('yyyy-MM-dd', n,)
    let todays = today + ' '
    let ToDAY = common.parseTime(todays, '{y}-{m}-{d}  星期{a}',)


    let time = common.dateFormat(e.detail.getTime(), 'yyyy-MM-dd')
    let selectTime = common.parseTime(time, '{y}-{m}-{d}  星期{a}',)
    var index = this.data.resultsArray.findIndex((item) => item.Java == selectTime)


    if (ToDAY === selectTime) {
      this.setData({
        todayNum: 0
      })
    } else {
      this.setData({
        todayNum: -1
      })
    }

    let length = this.data.resultsArray.length
    let arr = []
    arr = this.data.resultsArray
    let dateDisplayListArry = arr.slice(index, length)
    let currenTicketDate = {
      index: 0,
      item: {
        Java: selectTime
      }
    }
    this.setData({
      showCalendar: false,
      appointmentDate: selectTime,
      dateDisplayList: dateDisplayListArry,
      currenTicketDate
    }, () => {
      this.getTime(selectTime)
    })
  },
  time_to_sec(time) {
    if (time !== null) {
      var s = "";
      var hour = time.split(":")[0];
      var min = time.split(":")[1];
      s = Number(hour * 3600) + Number(min * 60)
      return s;
    }
  },
  // 如果time2大于time1 返回true 否则 返回false
  compareTime(time2, time1) {
    if (time2 - time1 > 0) {
      return true;
    } else {
      return false;
    }
  },

  // 选择日期
  openCalendar() {
    this.setData({
      showCalendar: true
    })
  },

  onCloseCalendar() {
    this.setData({
      showCalendar: false
    })
  },

})